'''
Created on 2011-2-24
Updated on 2011-3-2
@author: jeffrey
'''

from google.appengine.ext import db

'''
BaseModel
'''
class BaseModel(db.Model):
    createtime = db.DateTimeProperty(auto_now_add=True)
    
    @property
    def id(self):
        return str(self.key().id())

'''
1. User model store the user information.
User can not be deleted.
'''
class User(BaseModel):
    name = db.StringProperty(required=True)
    gender = db.IntegerProperty(choices=[0, 1], default=0)
    logo = db.LinkProperty()
    email = db.EmailProperty()
    lastlogtime = db.DateTimeProperty(auto_now=True)
    credit = db.IntegerProperty(default=0)

'''
2. SNS model store the SNS network information.
The model can only be edit by admin.
'''
class SNS(BaseModel):
    name = db.StringProperty(required=True)
    url = db.URLProperty(required=True)
    logo = db.LinkProperty()

'''
3. UserSNS model store the User and SNS relationship.
Delete SNS should delete the relationship.
'''
class UserSNS(BaseModel):
    user = db.ReferenceProperty(User)
    sns = db.ReferenceProperty(SNS)
    # The snsid store the user in SNS ID
    snsid = db.StringProperty()

'''
11. MessageType model store the message type data.
The model can only be edit by admin.
'''
class MessageType(BaseModel):
    name = db.StringProperty(required=True)
    logo = db.StringProperty()

'''
4. Message store the message information.
Only receiveuser can delete Message.
'''
class Message(BaseModel):
    title = db.StringProperty(required=True)
    messagetype = db.ReferenceProperty(MessageType)
    content = db.StringProperty(required=True, multiline=True)
    createuser = db.ReferenceProperty(User, collection_name='Message_createuser_set')
    isreceive = db.BooleanProperty(default=False)
    receiveuser = db.ReferenceProperty(User, collection_name='Message_receiveuser_set')
    receivetime = db.DateTimeProperty()

'''
5. Comment model store the Messages' comments information.
Delete Message should delete comments.
Should auth by the user.
'''
class Comment(BaseModel):
    message = db.ReferenceProperty(Message)
    user = db.ReferenceProperty(User)
    content = db.StringProperty(required=True, multiline=True)

'''
6. Tag model store the users' tag information.
'''
class Tag(BaseModel):
    name = db.StringProperty(required=True)
    user = db.ReferenceProperty(User)

'''
7. MessageTag model store the Message and Tag relationship.
Delete Message or Tag should delete the relationship.
'''
class MessageTag(BaseModel):
    message = db.ReferenceProperty(Message)
    tag = db.ReferenceProperty(Tag)

'''
8. Grade model store the system grade information.
The model can only be edit by admin.
'''
class Grade(BaseModel):
    name = db.StringProperty(required=True)
    # The max credit for this Grade.
    # If user credit > this credit, it'll go to next big Grade.
    credit = db.IntegerProperty(default=0)
    
    # canpost store how many messages that the user can post in the grade everyday.    
    canpost = db.IntegerProperty(default=0)
    
    # canreceive store how many messages that the user can receive in the grade everyday.
    canreceive = db.IntegerProperty(default=0)
    description = db.StringProperty(multiline=True)

'''
9. UserMessageStatistic model store the user message statistic data.
'''
class UserStatistic(BaseModel):
    user = db.ReferenceProperty(User)
    todaypost = db.IntegerProperty(default=0)
    todayreceive = db.IntegerProperty(default=0)
    totalpost = db.IntegerProperty(default=0)
    totalreceive = db.IntegerProperty(default=0)

'''
10. SystemParameter model store the system parameters data.
The model can only be edit by admin.
'''
class SystemParameter(BaseModel):
    pkey=db.StringProperty(required=True)
    pvalue=db.StringProperty(required=True)
    description = db.StringProperty(multiline=True)